/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nmp.controller;

import com.nmp.common.CommonUtil;
import com.nmp.common.RenderModel;
import com.nmp.common.UserData;
import com.nmp.model.SessionModel;
import com.nmp.model.UserModel;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author ngotandieu
 */
public class UserController extends HttpServlet {

    private static final Logger logger = Logger.getLogger(UserController.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        handle(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        handle(req, resp);
    }

    private void handle(HttpServletRequest req, HttpServletResponse resp) {
        try {
            CommonUtil.checkSession(req, resp);
            processs(req, resp);
        } catch (IOException ex) {
            logger.error("UserController.handle:" + ex.getMessage(), ex);
        }
    }

    private void processs(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String content;
        String pathInfo = (req.getPathInfo() == null) ? "" : req.getPathInfo();
        switch (pathInfo) {
            default:
                content = renderIndexPage(req, resp);
        }
        CommonUtil.out(content, resp);
    }

    private String renderIndexPage(HttpServletRequest req, HttpServletResponse resp) {
        String content = "";
        TemplateDataDictionary dic = TemplateDictionary.create();

        try {
            dic.setVariable("HEADER", CommonUtil.getHeader());
            dic.setVariable("FOOTER", CommonUtil.getFooter());
            dic.setVariable("MENU", CommonUtil.getMenu(req));

            SessionModel sessionModel = SessionModel.getInstance();
            String user_login_id = sessionModel.getUserLogin(req);
            UserModel userModel = UserModel.getInstance();
            UserData userData;
            if (user_login_id.startsWith("fb") || user_login_id.startsWith("gg")) {
                userData = userModel.getUserByAppUserId(user_login_id);
            } else {
                userData = userModel.getUserById(user_login_id);
            }
            
            
            Template template = RenderModel.getCTemplate("user_page");
            content = template.renderToString(dic);
        } catch (Exception ex) {
            logger.error("UserController.renderIndexPage: " + ex.getMessage(), ex);
        }
        CommonUtil.prepareHeader(resp, CommonUtil.HEADER_HTML);
        return content;
    }

}
